<template>
  <div class="app-container">
    <!--车间管理-->
    <div class="clearfix">
      <!--工具栏-->
      <div class="toolbar" style="float:left;">
        <el-form :inline="true" :model="filters" :size="size" @submit.native.prevent>
          <el-form-item>
            <el-input v-model="filters.name" placeholder="设备名称" />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              label="搜索"
              perms="sys:role:view"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
        </el-form>
      </div>
      <div class="toolbar" style="float:right">
        <el-form :inline="true" :size="size">
          <el-form-item>
            <el-button-group>
              <el-tooltip content="刷新" placement="top">
                <el-button icon="fa fa-refresh" @click="findPage(null)" />
              </el-tooltip>
              <!-- <el-tooltip content="导出" placement="top">
            <el-button icon="fa fa-file-excel-o"></el-button>
          </el-tooltip> -->
            </el-button-group>
          </el-form-item>
        </el-form>

      </div>
    </div>

    <!--表格内容栏-->
    <kt-table
      perms-edit="sys:user:edit"
      perms-delete="sys:user:delete"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
    >
      <template v-slot:buttonWrap>
        <!--添加按钮组-->
        <kt-button
          icon="fa fa-plus"
          label="添加"
          perms="sys:user:add"
          type="primary"
          @click="handleAdd"
        />
      </template>
      <!--      <template v-slot:ctrlButton="{data,index}">
        // 作用于插槽 操作按钮
        <kt-button
          icon="fa fa-plus"
          label="添加"
          perms="sys:user:add"
          type="primary"
          @click="handleAdd"
        />
      </template>-->
    </kt-table>

    <el-dialog
      :modal-append-to-body="false"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="700px"
      :close-on-click-modal="false"
      @closed="onCloseDialog('ruleForm')"
    >

      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="车间编号：" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="车间名称：" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属厂区" prop="factoryId">
              <el-select v-model="form.factoryId" filterable placeholder="请选择所属厂区">
                <el-option v-for="item in factoryList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="principalBy">
              <el-select v-model="form.principalBy" filterable placeholder="请选择负责人">
                <el-option v-for="user in userList" :key="user.id" :label="user.name" :value="user.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注：" prop="description">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="onConfrim">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import KtButton from '@/coreComponents/Core/KtButton'
import KtTable from '@/coreComponents/Core/KtTable'
import formMixin from '@/mixins/formMixin'
import { DialogTitle } from '@/utils/constant'
import userMixin from '@/mixins/userMixin'
export default {
  name: 'Workshop',
  components: {
    'kt-table': KtTable,
    'kt-button': KtButton
  },
  mixins: [formMixin, userMixin],
  data() {
    return {
      size: 'small',
      filters: {
        name: ''
      },
      columns: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      factoryList: [],
      /* dialog*/
      dialogVisible: false,
      formLabelWidth: '120px',
      dialogType: DialogTitle.ADD,
      form: {
        id: '',
        code: '',
        name: '',
        factoryId: '',
        principalBy: '',
        description: ''
      },
      rules: {
        code: [
          { required: true, message: '这是必填项', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '这是必填项', trigger: 'blur' }
        ],
        factoryId: [
          { required: true, message: '这是必选项', trigger: 'change' }
        ],
        principalBy: [
          { required: true, message: '这是必选项', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      return DialogTitle.getEnumName(this.dialogType) + '车间'
    }
  },
  created() {
    this.initColumns()
    this.getFactory()
    this.getUserList()
  },
  methods: {
    initColumns: function() {
      this.columns = [
        // { prop: 'id', label: 'ID', minWidth: 100 },
        { prop: 'code', label: '车间编号', minWidth: 120 },
        { prop: 'name', label: '车间名称', minWidth: 120 },
        { prop: 'VfactoryId', label: '所属厂区', minWidth: 120 },
        { prop: 'description', label: '描述', minWidth: 100 },
        { prop: 'VprincipalBy', label: '负责人', minWidth: 100 },
        { prop: 'createTime', label: '创建时间', minWidth: 180 }
      ]
      this.filterColumns = JSON.parse(JSON.stringify(this.columns))
    },

    // 获取厂房列表
    getFactory() {
      this.$api.systemBase.getFactoryList({}).then(res => {
        if (res.code === this.$code) {
          this.factoryList = res.data.content
        }
      })
    },

    // 获取分页数据
    findPage: function(data) {
      if (data !== null) {
        this.pageRequest = data.pageRequest
      }
      this.pageRequest.columnFilters = { name: { name: 'name', value: this.filters.name }}
      this.$api.systemBase.getWorkShopList(this.pageRequest).then((res) => {
        this.pageResult = res.data
        // 过滤数据
        this.pageResult.content.map(item => {
          item.createTime = this.formatterDate(item.createTime)
          return item
        })
      }).then(data != null ? data.callback : '')
    },
    onCloseDialog(name) {
      this.$refs[name].resetFields()
    },

    onConfrim() {
      const form = this.form
      this.$refs['ruleForm'].validate((valid) => {
        console.log(valid)
        if (valid) {
          this.loading = true
          this.$api.systemBase.postAddWorkShopInfo(form).then(res => {
            if (res.code === this.$code) {
              this.dialogVisible = false
              this.$message.success('添加成功')
              this.findPage(null)
            } else {
              this.$message.error(res.msg)
            }
          }).finally(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleAdd() {
      this.dialogType = DialogTitle.ADD
      this.dialogVisible = true
    },
    handleEdit({ index, row }) {
      this.dialogType = DialogTitle.EDIT
      this.dialogVisible = true
      this.$api.systemBase.getWorkShopInfo({ id: row.id }).then(res => {
        if (res.code === this.$code) {
          this.form = res.data
        }
      })
    },
    handleDelete(data) {
      this.$api.systemBase.postDelWorkShopInfo(data.params).then(data != null ? data.callback : '')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
